package com.edu.d2_stream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class StreamDemo {
    /**
     * 学习Stream流
     */
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        Collections.addAll(list,"张无忌","张三","赵敏","张三丰","力气","赵敏");
        System.out.println(list);
        //传统方法实现
/*        *//**
         * 1、取出姓张的名字，组成一个新的集合
         *//*
        List<String> zhanglist = new ArrayList<>();
        for (String s : list) {
            if(s.startsWith("张")){
                zhanglist.add(s);
            }
        }
        System.out.println(zhanglist);

        *//**
         * 2、取出姓张，且名字长度为3的名字
         *//*
        List<String> Threelist = new ArrayList<>();
        for (String s : zhanglist) {
            if(s.length()==3){
                Threelist.add(s);
            }
        }
        System.out.println(Threelist);*/

        //3、使用Stream流来实现,完整版
        list.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("张");
            }
        }).filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.length()==3;
            }
        }).forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        list.stream().filter(s -> s.startsWith("张")).filter(s -> s.length() == 3).forEach(s-> System.out.println(s));
        //Stream流转换成集合
        List<String> list1 = list.stream().filter(s -> s.startsWith("张")).filter(s -> s.length() == 3).collect(Collectors.toList());
        System.out.println(list1);
    }
}
